import Vue from 'vue'
import {
    strict
} from 'assert'

const state = {
    rangeThemeLayerColor:["FEE5D9", "FCAE91", "FB6A4A", "DE2D26", "A50F15"],
    activeModule: [], // 初始时激活的模块,
    showDataTable: false, // 空间分析-数据表格控制显示隐藏
    showEchartPanel: false, // 空间分析-图表控制显示隐藏
    showGroupPanel: false, // 空间分析-分组选择控制显示隐藏
    showStylePanel: false, // 空间分析-样式组件显示隐藏
    showTimeLinePanel: false, // 空间分析-时间轴显示隐藏
    showBaseMapLayer: false, // 空间分析-底图切花控制显示隐藏,
    showCollectInfo: false, // 空间分析-汇总信息显示隐藏
    showClassicTree: false, // 空间分析-条件筛选树
    showEmergencyList: false, // 空间分析-应急卫生资源-机构列表显示隐藏
    showMapControl: false, //地图控制器图层面板
    showInsInfo: false, // 空间分析-应急卫生资源-机构列表信息显示隐藏
    showPathOrPOI: false, // 空间分析-兴趣点或路径分析面板显隐
    periodType: "1",//报告期类型
    regionCodeArray: [], // 区划代码集合
    idenCode: "",//当前指标
    overLayerData: [],//专题图数组
    otherLegend: [],//分段专题图
    layerIdens: [],//专题图切换时使用的指标对应的指标分组数组
    currentLayerIdenValue: "",//专题图所展示的当前指标分组值
    currentLayerCode: "",//专题图所展示的指标code
    unionQueryCondition: {
        // "conditions":[],//分组条件集合
        'period_type': '1', // 时间维度类型
        'period_year_months': ['2017-0', '2018-0'], // 日期集合
        'region_codes': [] // 区划代码集合
    },
    currQuotaMetaData: null, // 当前选中的指标元数据
    currentIdenName: {},
    currQuotaGroups: [], // 当前指标选中的分组,用于工具条行政区划触发的查询

    quotaMetaDatas: [], // 选中的指标元数据集合，用于指标数更新时数据联动

    resultConditions: { // 返回结果中获取数据关联的时间区划等信息
        regionCodeMapNames: null, // 区划代码映射区划名称
        periods: [], // 时间轴，时间
        regionCodes: [], // 区划代码
        quotaCode: '' // 指标代码
    },
    currentRegionInfo: [],//查询到的所有区划
    resultConditionsArr: [], // 保存每次叠加时的查询返回条件

    quotasVersionsDatas: [], // 所有勾选指标的数组
    currentTime: '2019-0', // 时间轴播放的当前时间
    startTime: "",//最早的时间
    dataPanelTitle: null, // 数据表格表头
    dataPanelTitleArr: [], // 保存多个指标的数据表格表头
    formateTableData: null, // 格式化的表格数据，用于生成图表
    currentIdens: [],//当前所有区划的集合
    currentQuotaLayerData: null, // 当前最上级的图层数据,
    pathOrpoiStyle: 'left:30px', // 路径分析&POI页面样式
    currentYearMonth: "",
    yearMonthList: [],
    isDataList: [],
    mapLayers: [],//专题图层
    barLayerIden: [],
    allLayer: [],
    currentRegionXY: [],//处理chart带坐标的区划
    idenThemerange:[],//已选择的指标
    mapLayeriden:{},//当前对应专题图选择的指标
    reportType:1,
    chartValue:null,
    showGroup:false,
    layerColors:{},
    currentIdenThemanger:"",//当前指标值
    currentIdenrangeThemanger:"",
    themerangeIden:"",//分段图当前指标用于时间轴播放
    enlargeTable:false,//放大表格
    enlargeChart:false,//放大统计图
    currentChartType:"line",
    play:false,//控制播放
}

const getters = {

}

const mutations = {
    keepRangeThemeLayerColor(state,colors){
        state.rangeThemeLayerColor = colors
    },
    changePlay(state,play){
        state.play = play
    },
    currentChartType(state,type){
        state.currentChartType = type
    },
    changeEnlargeTable(state,status){
        state.enlargeTable = status
    },
    changeEnlargeChart(state,status){
        state.enlargeChart = status
    },
    themerangeIden(state,value){
        state.themerangeIden = value
    },
    currentIdenThemanger(state,value){
        state.currentIdenThemanger = value
    },
    currentIdenrangeThemanger(state,value){
        state.currentIdenrangeThemanger = value
    },
    layerColors(state,pick){
        state.layerColors[pick.type] = pick.color
    },
    changeShowGroup(state,show){
        state.showGroup = show
    },
    chartValue(state,value){
        state.chartValue = value
    },
    showIndexType(state,type){
        state.reportType = type
    },
    idenChange(state, iden) {
        let iscreat = state.idenThemerange.some(item => {
            if (item.id == iden) {
                return true;
            }
        });
        if (!iscreat) {
            state.idenThemerange.push(iden)
        }
    },
    getMapLayeriden(state,layer){
        state.mapLayeriden[layer.type] = layer.iden
    },
    deletIden(state, iden) {
        state.idenThemerange.map((item, index) => {
            if (item == iden) {
                state.idenThemerange.splice(index, 1)
            }
        })
    },
    addLayer(state, layer) {
        let iscreat = state.allLayer.some(item => {
            if (item == layer) {
                return true;
            }
        });
        if (!iscreat) {
            state.allLayer.push(layer)
        }
    },
    deletLayer(state, layer) {
        state.allLayer.map((item, index) => {
            if (item == layer) {
                state.allLayer.splice(index, 1)
            }
        })
    },
    clearLayer(state){
        state.allLayer = []
    },
    getBarLayerIden(state, arr) {
        state.barLayerIden = arr
    },
    getCurrentRegionXY(state, arr) {
        state.currentRegionXY = arr
    },
    getMapLayers(state, layer) {
        let iscreat = state.mapLayers.some(item => {
            if (item.type == layer.type) {
                layer.ident = item.ident
                item = layer
                return true;
            }
        });
        if (!iscreat) {
            state.mapLayers.push(layer)
        }
    },
    deletMapLayers() {
        state.mapLayers = [];
    },
    getYearMonthData(state, time) {
        state.isDataList = time
    },
    getYearMonth(state, time) {
        state.yearMonthList = time
    },
    changeCurrentYearMonth(state, time) {
        state.currentYearMonth = time
    },
    changCurrentIdenName(state, data) {
        state.currentIdenName[data.type] = data.value
    },
    deleteIdenName(state){
        state.currentIdenName = {}
    },
    getReigonInfo(state, value) {
        state.currentRegionInfo = value
    },
    changeLayerIdenValue(state, value) {
        state.currentLayerIdenValue = value
    },
    changeLayerIdenCode(state, value) {
        state.currentLayerCode = value
    },
    changeLayerIdens(state, idens) {
        state.layerIdens = idens
    },
    setStartTime(state, time) {
        state.startTime = time
    },
    addOtherLegend(state, legend) {
        state.otherLegend = legend
    },
    deletOtherLegend(state, legend) {
        state.otherLegend = ""
    },
    // 去除checkbox选中状态时，请求title
    deleteDataPanelTitle(state, quota) {
        if (state.dataPanelTitleArr) {
            state.dataPanelTitleArr.map((item, index) => {
                if (item.quotaCode === quota.iden_code) {
                    state.dataPanelTitleArr.splice(index, 1)
                }
            })
            state.dataPanelTitle = state.dataPanelTitleArr[state.dataPanelTitleArr.length - 1]
        } else {
            state.dataPanelTitle = null
        }
    },
    addDataPanelTitle(state, dataPanelTitle) {
        let flag = false
        state.dataPanelTitleArr.map((item, i) => {
            if (item.quotaCode == dataPanelTitle.quotaCode) {
                state.dataPanelTitleArr[i] = dataPanelTitle
                flag = true
            }
        })
        if (!flag) {
            state.dataPanelTitleArr.push(dataPanelTitle)
        }
        // 设置当前表头
        state.dataPanelTitle = dataPanelTitle
    },
    addOverLayerData(state, layer) {
        if (!state.overLayerData.length) {
            state.overLayerData.push(layer)
        } else {
            state.overLayerData.map((item) => {
                if (item.layerId != layer.layerId) {
                    state.overLayerData.push(layer)
                }
            })
        }
    },
    deleOverLayerData(state, layer) {
        state.overLayerData = [];
    },
    setCurrentIdenCode(state, code) {
        state.idenCode = code
    },
    changePeriodType(state, type) {
        state.periodType = type
    },
    addQuotaMetaDatas(state, quota) {
        state.currQuotaMetaData = quota
    },
    changeCurrQuotaGroups(state, groups) {
        state.currQuotaGroups = groups
    },
    changeIdens(state, groups) {
        state.currentIdens = groups
    },
    changeFormateTableData(state, tabledata) {
        state.formateTableData = tabledata
    },
    changeDataPanelTitle(state, titleArr) {
        state.dataPanelTitle = titleArr
    },
    changeCurrentTime(state, time) {
        state.currentTime = time
    },
    changeRegionCodeMapNames(state, data) {
        state.resultConditions = data
    },
    addResultConditions(state, data) {
        let quotaCode = state.currQuotaMetaData.iden_code
        state.resultConditionsArr.map((item, i) => {
            if (item.quotaCode == quotaCode) {
                state.resultConditionsArr.splice(i, 1)
            }
        })
        state.resultConditionsArr.push(data)
        state.resultConditions = data
    },
    deleteResultConditions(state, data) {
        if (state.resultConditionsArr) {
            state.resultConditionsArr.map((item, i) => {
                if (item.quotaCode == data.iden_code) {
                    state.resultConditionsArr.splice(i, 1)
                }
            })
            state.resultConditions = state.resultConditionsArr[state.resultConditionsArr.length - 1]
        } else {
            state.resultConditions = null
        }
    },
    addQuota(state, data) { // 增加指标数据
        let quotaCode = state.currQuotaMetaData.iden_code
        state.quotasVersionsDatas.map((item, i) => {
            if (item[quotaCode]) {
                state.quotasVersionsDatas.splice(i, 1)
            }
        })
        state.quotasVersionsDatas.push(data)
        // 将新添加的指标赋值存一下
        state.currentQuotaLayerData = data
    },
    removeQuota(state, data) { // 移除指标数据
        if (state.quotasVersionsDatas) {
            state.quotasVersionsDatas.map((item, i) => {
                if (item[data.iden_code]) {
                    state.quotasVersionsDatas.splice(i, 1)
                }
            })
            state.currentQuotaLayerData = state.quotasVersionsDatas[state.quotasVersionsDatas.length - 1]
        } else {
            state.currentQuotaLayerData = null
        }
    },
    updataQuota(state, data) { // 选择数组后更新指标数据
        state.currentQuotaLayerData = data
    },
    changeQuotaZindex(state, value) { // 修改图层zindex后修改数组的顺序
        if (state.quotasVersionsDatas) {
            let id = value.id
            let type = value.type
            let datas = state.quotasVersionsDatas
            for (let i = 0; i < datas.length; i++) {
                if (datas[i][id]) {
                    let item = JSON.parse(JSON.stringify(datas[i]))
                    if (type === 'up') {
                        datas.splice(i, 1)
                        datas.splice(datas.length, 0, item)
                    } else {
                        datas.splice(i, 1)
                        datas.splice(0, 0, item)
                    }
                    state.currentQuotaLayerData = datas[datas.length - 1]
                }
                break
            }
        }
    },
    reSetStoreData(state) {
        state.unionQueryCondition.conditions = []
        state.unionQueryCondition.period_type = '1'
        state.unionQueryCondition.period_year_months = ['2017-0', '2018-0']
        state.unionQueryCondition.region_codes = []

        state.addResultConditions = []
        state.resultConditions = null
        state.resultConditionsArr = []
        state.quotaMetaDatas = []
        state.currQuotaGroups = []
        state.dataPanelTitleArr = []
        state.currQuotaMetaData = null
        state.currentQuotaLayerData = null
        state.quotasVersionsDatas = []
    },
    /**
     *
     * @param {*} state
     * @param {*} object {key:unionQueryCondition中的属性 , val: 属性的值}
     */
    setUnionQueryCondition(state, object) {
        state.unionQueryCondition[object.key] = object.val
        state.unionQueryCondition["period_type"] = object.type
    },
    changeRegionCodeArray(state, arr) {
        state.regionCodeArray = arr
    },
    changeBaseMapLayer(state, flag) {
        state.showBaseMapLayer = flag
    },
    changeCollectInfo(state, flag) {
        state.showCollectInfo = flag
    },
    changeClassicTree(state, flag) {
        state.showClassicTree = flag
        if (flag) {
            state.pathOrpoiStyle = 'left:350px'
        } else {
            state.pathOrpoiStyle = 'left:30px'
        }
    },
    changeShowTimeLine(state, flag) {
        state.showTimeLinePanel = flag
    },
    changeShowStylePanel(state, flag) {
        state.showStylePanel = flag
    },
    changeShowGroupPanel(state, flag) {
        state.showGroupPanel = flag
    },
    changeShowEchartPanel(state, flag) {
        state.showEchartPanel = flag
    },
    changeShowDataTable(state, flag) {
        state.showDataTable = flag
    },
    changeActiveModule(state, acModule) {
        state.activeModule = acModule
    },
    changeEmergencyListLayer(state, flag) {
        state.showEmergencyList = flag
    },
    changeInsInfoLayer(state, flag) {
        state.showInsInfo = flag
    },
    changePathOrPOI(state, flag) {
        state.showPathOrPOI = flag
    },
    //图层控制面板
    changeShowMapControl(state, flag) {
        state.showMapControl = flag;
    }
}

export default {
    state,
    getters,
    mutations
}
